package com.xd.core.business.controller;


import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.util.AESUtil;
import com.xd.common.util.SecurityUtil;
import com.xd.core.business.dto.ContractDto;
import com.xd.core.business.service.IContractService;
import com.xd.core.business.vo.ContractDetailVo;
import com.xd.core.business.vo.ContractVo;
import com.xd.core.business.vo.DeContractVo;
import com.xd.core.business.vo.IncomingDetailContractVo;
import com.xd.core.mission.entity.MissionCenterInfo;
import com.xd.core.mission.mapper.MissionCenterInfoMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2025-02-27
 */
@Api(tags = "合同管理")
@RestController
@RequestMapping("/api/contract")
public class ContractController {

    @Autowired
    private IContractService iContractService;
    @Autowired
    private MissionCenterInfoMapper missionCenterInfoMapper;


    @PreAuthorize("hasAnyAuthority('sys:LoanCustomerContract-viewDetails','sys:LoanCustomerContract')")
    @ApiOperation("借款合同列表")
    @PostMapping("/contractList")
    public Page<ContractVo> getContractList(@RequestBody ContractDto contractDto) {
        return iContractService.getContractList(contractDto);
    }

    public static void main(String[] args) {
        String a ="MoPCr8agnHignev6wQGx4g==";
        String decrypt = AESUtil.decrypt(a, "bd482fcff91d47cc");
        System.out.println(decrypt);
    }

    @ApiOperation("导出借款合同列表")
    @PostMapping("/exportContractList")
    public void exportContractList(@RequestBody ContractDto contractDto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);

        iContractService.exportContractList(contractDto, missionCenterInfo.getId());
    }
    @PreAuthorize("hasAnyAuthority('sys:DebtTransferContractManager-viewDetails','sys:DebtTransferContractManager')")
    @ApiOperation("债券转让合同列表")
    @PostMapping("/deContractList")
    public Page<DeContractVo> getDeContractList(@RequestBody ContractDto contractDto) {
        return iContractService.getDetContractList(contractDto);
    }


    @ApiOperation("导出债券转让合同列表")
    @PostMapping("/exportDeContractList")
    public void exportDeContractList(@RequestBody ContractDto contractDto) {
        //插入数据状态是正在导出
        MissionCenterInfo missionCenterInfo = new MissionCenterInfo();
        missionCenterInfo.setCompanyId(SecurityUtil.getLoginCompanyId());
        missionCenterInfo.setFileStatus(SysConstant.YSZT_FILE_STATUS_ONE);
        missionCenterInfo.setCreateTime(new Date());
        missionCenterInfo.setUserId(SecurityUtil.getLoginUserId());
        missionCenterInfoMapper.insert(missionCenterInfo);

        iContractService.exportDeContractList(contractDto, missionCenterInfo.getId());
    }

    @ApiOperation("业务详情--合同列表")
    @GetMapping("/getContracts/{outId}")
    public IncomingDetailContractVo getContracts(@PathVariable Integer outId) {
        return iContractService.getContracts(outId);
    }


    @ApiOperation("保单贷--业务详情--合同列表")
    @GetMapping("/getInsuranceContracts/{incomingId}")
    public List<ContractDetailVo> getInsuranceContracts(@PathVariable Integer incomingId) {
        return iContractService.getInsuranceContracts(incomingId);
    }
}
